package com.fr.lintcode;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

/**
*作者：furong
*日期：2016年12月23日
*时间：下午4:29:00
*/
public class Q171 {
    int prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 53, 59, 83, 89, 31, 37, 61, 67, 41, 43, 47, 71, 73, 101, 103, 107 };

    public List<String> anagrams(String[] strs) {
        if (strs == null || strs.length == 0) {
            return null;
        }
        List<Long> list = new ArrayList<>();
        List<Integer> hasInsert = new ArrayList<>();
        List<String> rs = new ArrayList<>();
        for (String s : strs) {
            long l = calculate(s);
            int index = list.indexOf(l);
            if (index >= 0) {
                if (hasInsert.contains(index)) {
                    rs.add(s);
                } else {
                    rs.add(strs[index]);
                    rs.add(s);
                    hasInsert.add(index);
                }

            }
            list.add(l);
        }
        return rs;
    }

    private long calculate(String s) {
        if (s == null) {
            return -1;
        }
        long rs = 1;
        for (int i = 0; i < s.length(); i++) {
            rs = rs * prime[s.charAt(i) - 'a'];
        }
        return rs;

    }

    @Test
    public void TestA() {
        System.out.println(prime.length);
        String[] strs = { "lint", "intl", "inlt", "code", "coed" };
        List<String> rs = anagrams(strs);
        System.out.println(rs);
    }
}
